import message from '@/lib/message';
import utils from '@/lib/utils';
import config from "@/config";
import storage from "./storage/storage";
import PSocket from "./socket/PSocket";

class AppSocket {
    private socket!: PSocket;
    /**
     * 初始化
     */
    public init() {
        if (this.socket) {
            console.log(this.socket.GetReadyStateName());
            return;
        }
        console.log("socket 连接中……");
        const user = storage.user.Get();
        if (user.userId) {
            // 创建 socket
            this.socket = new PSocket(config.socketHost, "/chart/ws");
            // 连接socket
            this.socket.Connect({
                openFunc: this.onopen,
                successMessage: this.successMsg,
                errorMessage: this.errorMsg,
                errorFunc: this.onerror,
                closeFunc: this.onclose,
            });
        }
    }

    /**
     * 发送消息
     */
    public sendMsg(type: any, data: any) {
        this.socket.SendMessage(type, data);
    }
    // 监听socket连接
    private onopen(socket: PSocket, e: any) {
        console.log("socket", socket.GetReadyStateName());
    }
    // 监听socket错误信息
    private onerror(socket: PSocket, e: any) {
        console.log("socket", socket.GetReadyStateName());
        // appSocket.reConnect();
    }
    // socket销毁监听
    private onclose(socket: PSocket, e: any) {
        console.log("socket", socket.GetReadyStateName());
        // appSocket.reConnect();

    }
    // 错误消息
    private errorMsg({ type, data, info }: any) {
        console.log("socket错误消息", type, data);
    }
    // 成功消息
    private successMsg({ type, data, info }: any) {
        // console.log("%c┏━━━━━━━━━━━━━socket消息━━━━━━━━━━━┓", "color:#F100F8");
        // console.log("%c消息类型：", "color:#F100F8", type);
        // console.log("%c消息内容：", "color:#F100F8", data);
        // console.log("%c┗━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━┛", "color:#F100F8");
        switch (type) {
            case "jcj-ring": // 电话
                break;
            case "jcj-bj": // 警情
                break;
            case "jcj-dxdw": // 短信定位
                break;
            case "jcj-gps": // 设备定位定位
                break;
            case 3: // 消息通知
                break;
            case 4: // 勤务通知
                break;
            case 5: // 调度指令
                break;
            case 6: // 预警信息
                break;
            case 7: // 督察信息提示
                break;
            case 8: // 定位变动
                break;
        }
    }
}

const appSocket: AppSocket = new AppSocket();
export default appSocket;
